package com.zhiyou100.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou100.annotation.Auth;
import com.zhiyou100.model.Speaker;
import com.zhiyou100.serice.SpeakerService;
import com.zhiyou100.util.FastDFSUtil;
import com.zhiyou100.util.ResultObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.List;

@Controller
public class SpeakerController {
    @Autowired
    private SpeakerService service;
    @Autowired
    private JedisPool jedisPool;
    @RequestMapping(value = "/speaker",method = RequestMethod.POST)
    @ResponseBody @Auth("这是一个查询全部speaker的操作")
    public ResultObject findAll(Integer pageNo){
        if(pageNo == null){
            pageNo =1;
        }
        PageHelper.startPage(pageNo,3);
        List<Speaker> speakers = service.findAll();
        PageInfo<Speaker> pageInfo = new PageInfo<Speaker>(speakers);
        ArrayList<Object> lists = new ArrayList<Object>();
        lists.add(speakers);
        lists.add(pageInfo);
        return new ResultObject("200","查询全部speaker",lists);
    }
    @RequestMapping(value = "/speaker/pageInfo/{id}",method = RequestMethod.POST)
    @ResponseBody @Auth("这是一个查询全部speaker进行分页的操作")
    public ResultObject findAllPageInfo(@PathVariable("id") Integer id){
            return findAll(id);
    }

    @RequestMapping(value = "/speaker/{id}",method = RequestMethod.POST)
    @ResponseBody @Auth("这是一个通过id查询speaker的操作")
    public ResultObject findById(@PathVariable("id") Integer id){
        Speaker speaker = service.findById(id);
        return new ResultObject("200","查询一个speaker",speaker);
    }
    @RequestMapping(value = "/speaker/{id}",method = RequestMethod.PUT)
    @ResponseBody @Auth("这是一个更新一个speaker的操作")
    public ResultObject update(@PathVariable("id") Integer id,@RequestBody Speaker speaker){
        speaker.setId(id);
        System.out.println(speaker);
        service.update(speaker);
        Jedis resource = jedisPool.getResource();
        resource.del("*");
        return new ResultObject();
    }
    @RequestMapping(value = "/speaker/insert",method = RequestMethod.POST)
    @ResponseBody@Auth("这是一个添加一个speaker的操作")
    public ResultObject add(@RequestBody Speaker speaker){
        service.add(speaker);
        Jedis resource = jedisPool.getResource();
        resource.del("*");
        return new ResultObject();
    }
    @RequestMapping(value = "/speaker/{id}",method = RequestMethod.DELETE)
    @ResponseBody@Auth("这是一个删除一个speaker的操作")
    public  ResultObject delete(@PathVariable("id") Integer id){
        service.delete(id);
        Jedis resource = jedisPool.getResource();
        resource.del("*");
        return new ResultObject();
    }
    @RequestMapping(value = "/speaker/fileupload",method = RequestMethod.POST)
    @ResponseBody@Auth("这是一个图片上传在speaker上的操作")
    public  ResultObject fileupload(MultipartFile imgUrl) throws Exception {
        String upload = FastDFSUtil.upload(imgUrl);
        return new ResultObject("200","文件上传完毕",upload);
    }


}
